package com.igeek.car.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.car.constant.MessageConstant;
import com.igeek.car.entity.User;
import com.igeek.car.service.UserService;
import com.igeek.car.result.Result;
import com.igeek.car.vo.PageQueryVO;
import com.igeek.car.vo.UserStatusVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
@Api(tags = "用户信息管理")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("分页条件查询用户信息")
    @GetMapping("/page")
    public Result findPage(PageQueryVO vo) {
        Page<User> page = userService.findPage(vo);
        return new Result(true, MessageConstant.QUERY_USER_SUCCESS, page);
    }

    @ApiOperation("新增用户")
    @PostMapping
    public Result add(@RequestBody User user) {
        // 检查用户名是否已存在
        User existUser = userService.findByUsername(user.getUsername());
        if (existUser != null) {
            return new Result(false, "用户名已存在");
        }

        boolean flag = userService.save(user);
        return new Result(flag, flag ? MessageConstant.ADD_USER_SUCCESS : MessageConstant.ADD_USER_FAIL);
    }

    @ApiOperation("根据ID查询用户")
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
    }

    @ApiOperation("更新用户信息")
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody User user) {
        user.setUserId(id);
        boolean flag = userService.updateById(user);
        return new Result(flag, flag ? MessageConstant.UPDATE_USER_SUCCESS : MessageConstant.UPDATE_USER_FAIL);
    }

    @ApiOperation("更新用户状态")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable Integer id, @RequestBody UserStatusVO statusVO) {
        boolean flag = userService.updateStatus(id, statusVO.getStatus());
        return new Result(flag, flag ? MessageConstant.UPDATE_USER_SUCCESS : MessageConstant.UPDATE_USER_FAIL);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = userService.removeById(id);
        return new Result(flag, flag ? MessageConstant.DELETE_USER_SUCCESS : MessageConstant.DELETE_USER_FAIL);
    }

    @ApiOperation("检查用户名是否存在")
    @GetMapping("/exists/username")
    public Result checkUsernameExists(@RequestParam String username) {
        boolean exists = userService.findByUsername(username) != null;
        return new Result(true, exists ? "用户名已存在" : "用户名可用", exists);
    }

    @ApiOperation("检查邮箱是否存在")
    @GetMapping("/exists/email")
    public Result checkEmailExists(@RequestParam String email) {
        boolean exists = userService.findByEmail(email) != null;
        return new Result(true, exists ? "邮箱已存在" : "邮箱可用", exists);
    }

    @ApiOperation("检查手机号是否存在")
    @GetMapping("/exists/phone")
    public Result checkPhoneExists(@RequestParam String phone) {
        boolean exists = userService.findByPhone(phone) != null;
        return new Result(true, exists ? "手机号已存在" : "手机号可用", exists);
    }

}